import cv2
import numpy as np

target_size = (250, 250)

# 读取训练图像并调整大小
face0_1 = cv2.imread('faces/liujiarui11.jpg', 0)
face0_2 = cv2.imread('faces/liujiarui12.jpg', 0)
face1_1 = cv2.imread('faces/liujiarui1.jpg', 0)
face1_2 = cv2.imread('faces/liujiarui2.jpg', 0)

face0_1 = cv2.resize(face0_1, target_size)
face0_2 = cv2.resize(face0_2, target_size)
face1_1 = cv2.resize(face1_1, target_size)
face1_2 = cv2.resize(face1_2, target_size)

train_images = [face0_1, face0_2, face1_1, face1_2]
labels = np.array([1, 1, 2, 2])

nameLabels = {1: "fujianjun", 2: "liujiarui"}

# 创建并训练EigenFace识别器
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.train(train_images, labels)

# 读取测试图像为彩色图像
gray = cv2.imread('faces/liujiarui13.jpg')


img4 = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)

label, confidence = recognizer.predict(img4)
print(nameLabels[label],end=" ")
print(confidence)

# 在原图像上绘制矩形框和标签

# 显示结果图像
cv2.imshow("img", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
